package com.ppt.convert.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.ppt.convert.domain.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author zcx
 * @Title 下载文件
 * @date 2020年06月30日 9:58
 **/
@Service
public class DownLoadService {

    private static final Logger logger = LoggerFactory.getLogger(DownLoadService.class);

    @Value("${file.afterAbsoluteFolder}")
    private String afterAbsoluteFolder;

    public Result downLoadFile(String path,String fileUrl,String dentryId,String fileName) {
        try {
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs(); //创建目录
            }
            //带进度显示的文件下载
            long size = HttpUtil.downloadFile(fileUrl, FileUtil.file(path+File.separator+fileName), new StreamProgress(){

                @Override
                public void start() {
                    logger.info("正在从网盘开始下载。。。。");
                }

                @Override
                public void progress(long progressSize) {
                    logger.info("已下载：{}", FileUtil.readableFileSize(progressSize));
                }

                @Override
                public void finish() {
                    logger.info("从网盘下载完成！");
                }
            });
            return Result.succeed(size,"文件下载成功");
        }catch (Exception e) {
            return Result.failed(0,"文件下载失败"+e);
        }

    }

}
